<?php
namespace app\member\controller;
use app\member\controller\Export;

class Resource extends Common
{
    public function index()
    {	
    	if(request()->isAjax()){
    		$account = input('account');
    		$level_id = input('level_id');
    		$total_loan_count = $this->is_export($level_id);
    		
    		$judging_balance = $this->balance($level_id,$account);

    		if(!$judging_balance['bool']){
	    		$this->error('余额不足!');
	    	}

    		if($account<=0){
    			$this->error('请选择正确数据格式!');
    		}

    		if($account > $total_loan_count){
    			$this->error('没有数据可导或超过可导数量!');
    		}
    		$this->success();
    	}

    	//查询用户剩余多少数据导出
    	$loan_level = db('loan_level')->select();
    	foreach ($loan_level as $key => $value) {
    		$loan_level[$key]['num'] = $this->is_export($value['id']);
    	}

    	$this->assign('loan_level',$loan_level);
        return $this->fetch();
    }

    /**
	 * 商户导出excel
	 */
	public function export()
	{
		$account = input('account');
		$level_id = input('level_id');

		$total_loan_count = $this->is_export($level_id);

		if($account > $total_loan_count){
    		$this->error('没有数据可导或超过可导数量!');
    	}

    	$judging_balance = $this->balance($level_id,$account);
    	if(!$judging_balance['bool']){
    		$this->error('余额不足!');
    	}

		$headArr = array("姓名","手机号码","城市","性别","年龄","贷款额度","信用度","房产","车产",'职业','发薪方式','收入','公积金','社保','贷款期限');

		$loan_export_record = db('loan_export_record')->where(['uid'=>$this->info['id']])->select();
		$lid = array_column($loan_export_record, 'lid');

		$list = db("loan_info")->where(['id'=>['not in',$lid],'level_id'=>$level_id])->limit($account)->select();

		$data=array();
		$lid_array = [];
		foreach($list as $k => $v){
			$data[$k][] = $v['name'];
			$data[$k][] = $v['mobile'];
			$data[$k][] = $v['city'];
			$data[$k][] = $v['gender'];
			$data[$k][] = $v['age'];
			$data[$k][] = $v['loan_amount'];
			$data[$k][] = $v['credit_card'];
			$data[$k][] = $v['property'];
			$data[$k][] = $v['car_production'];
			$data[$k][] = $v['career'];
			$data[$k][] = $v['pay_method'];
			$data[$k][] = $v['income'];
			$data[$k][] = $v['provident_fund'];
			$data[$k][] = $v['social_security'];
			$data[$k][] = $v['loan_term'];

			$lid_array[] = $v['id'];
			$insert_data[$k]['lid']=$v['id'];
			$insert_data[$k]['uid']=$this->info['id'];
			$insert_data[$k]['creatime']=time();
		}

		db('loan_export_record')->insertAll($insert_data);

		//扣取客户款向
		$amount_log_data['user_id'] = $this->info['id'];
		$amount_log_data['type'] = '-1';
		$amount_log_data['price'] = $judging_balance['total_price'];
		$amount_log_data['lid'] = json_encode($lid_array);
		$amount_log_data['createtime'] = time();
		$amount_log_data['each_price'] = $judging_balance['price'];
		$amount_log = db('amount_log')->insert($amount_log_data);
		if($amount_log){
			db('admin_member')->where(['id'=>$this->info['id']])->setDec('balance',$judging_balance['total_price']);
		}

		$filename='客户数据-'.date("Y-m-d H:i:s");
		(new Export())->ImportExcel($filename,$headArr,$data);
	}

	/**
	*可导数据
	*/
	public function is_export($level_id){
		if(empty($level_id))return 0;
		$loan_info_count = db('loan_info')->where(['level_id'=>$level_id])->count();
    	$loan_export_record_count = db('loan_export_record')->alias('le')->join('loan_info li','li.id=le.lid')->where(['le.uid'=>$this->info['id'],'li.level_id'=>$level_id])->count();
    	$total_loan_count = $loan_info_count - $loan_export_record_count;
    	return $total_loan_count;
	}

	/**
	*计算用户是否够钱导出
	*/
	public function balance($level_id,$num){
		$balance = db('admin_member')->where(['id'=>$this->info['id']])->value('balance');

		$price = db('loan_level')->where(['id'=>$level_id])->value('price');

		$total_price = $price*$num;

		$result = [];

		if($total_price<=$balance){
			$result['bool'] = true;
		}else{
			$result['bool'] = false;
		}

		$result['total_price'] = $total_price;
		$result['price'] = $price;
		return $result;
	}
}
